//
//  MySetting.m
//  HappyStore
//
//  Created by 李小辉 on 16/8/16.
//  Copyright © 2016年 LLJ. All rights reserved.
//

#import "MySetting.h"
#import "mainMyCell.h"
#import "ForgetPassWord.h"
#import "my_fankui.h"
#import "qlPreviewController.h"
@interface MySetting ()

@end

@implementation MySetting

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"白色背景"] forBarMetrics:UIBarMetricsDefault];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 190, 44)];
    titleLabel.backgroundColor = [UIColor clearColor];
    titleLabel.font = [UIFont systemFontOfSize:19];
    if ([self.changePasswordStr isEqualToString:@"修改密码"]) {
        titleLabel.text = @"修改密码";
    }else{
        titleLabel.text = @"设置";
    }
    titleLabel.textColor = RGB(45, 45, 45);
    titleLabel.textAlignment = NSTextAlignmentCenter;
    self.navigationItem.titleView = titleLabel;
    
    self.tableView.bounces = NO;
    //设置返回按钮
    UIBarButtonItem *leftItem = [UIBarButtonItem itemWithTarget:self action:@selector(leftBarButtonItemClick) image:@"返回" highImage:@"返回"];
    self.navigationItem.leftBarButtonItem = leftItem;
    [self setExtraCellLineHidden:self.tableView];
    
    //开启侧滑
    self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
}
//去掉多余的表格线
- (void)setExtraCellLineHidden:(UITableView *)tableView{
    UIView *view = [UIView new];
    view.backgroundColor = [UIColor clearColor];
    [tableView setTableFooterView:view];
}
-(void)leftBarButtonItemClick{
    [self.navigationController popViewControllerAnimated:YES];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
  
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if ([self.changePasswordStr isEqualToString:@"修改密码"] ) {
        return 1;
    }else
        return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if ([self.changePasswordStr isEqualToString:@"修改密码"] ) {
        return 2;
    }else{
        if (section == 0) {
            return 2;
        }else if (section == 1){
            return 3;
        }else
            return 1;
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    mainMyCell *cell = [[mainMyCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"reuseIdentifier"];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    if ([self.changePasswordStr isEqualToString:@"修改密码"] ) {
        if (indexPath.row == 0) {
            cell.textLabel.text = @"登录密码";
        }else
            cell.textLabel.text = @"支付密码";
    }else{
        switch (indexPath.section) {
            case 0:
                if (indexPath.row == 0) {
                    cell.textLabel.text = @"修改密码";
                }else{
                    cell.accessoryType = UITableViewStylePlain;
                    UISwitch *switchClick = [[UISwitch alloc] initWithFrame:CGRectMake(screenW-62, 11, 50, 30)];
                    NSLog(@"%@",[USER_DEFAULTS objectForKey:@"isOpenSound"]);
                    if ([[USER_DEFAULTS objectForKey:@"isOpenSound"] boolValue]) {
                        switchClick.on = YES;
                    }else{
                        switchClick.on = NO;
                    }
                    [switchClick addTarget:self action:@selector(switckClick:) forControlEvents:UIControlEventValueChanged];

                
                    [cell addSubview:switchClick];
                    cell.textLabel.text = @"铃音通知";
                }
                break;
            case 1:
                if (indexPath.row == 0) {
                    cell.textLabel.text = @"关于我们";
                }else if(indexPath.row == 1){
                    cell.textLabel.text = @"用户协议";
                }else{
                    cell.textLabel.text = @"帮助和反馈";
                }
                break;
            case 2:
                cell.textLabel.text = @"退出帐号";
            default:
                break;
        }
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if ([self.changePasswordStr isEqualToString:@"修改密码"] ) {
        if (indexPath.row == 0) {//修改登录密码
            UIStoryboard *LoginAndRegister = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
            ForgetPassWord *ForgetPassWord = [LoginAndRegister instantiateViewControllerWithIdentifier:@"forgetpassword"];
            ForgetPassWord.titleStr = @"修改登录密码";
            [self.navigationController pushViewController:ForgetPassWord animated:YES];
        }else{//修改支付密码
            UIStoryboard *LoginAndRegister = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
            ForgetPassWord *ForgetPassWord = [LoginAndRegister instantiateViewControllerWithIdentifier:@"forgetpassword"];
            ForgetPassWord.titleStr = @"修改支付密码";
            [self.navigationController pushViewController:ForgetPassWord animated:YES];
        }
    }else{
        switch (indexPath.section) {
            case 0:
                if (indexPath.row == 0) {//修改密码
                    MySetting *possWordVC = [[MySetting alloc] init];
                    possWordVC.changePasswordStr = @"修改密码";
                    [self.navigationController pushViewController:possWordVC animated:YES];
                }else{//铃音通知
                    
                }
                break;
            case 1:
                if (indexPath.row == 0) {//关于我们
                    qlPreviewController *qlview = [[qlPreviewController alloc] init];
                    qlview.chickStr = @"关于我们";
                    qlview.currentPreviewItemIndex = 0;
                    [self.navigationController pushViewController:qlview animated:YES];
                }else if(indexPath.row == 1){//用户协议
                    qlPreviewController *qlview = [[qlPreviewController alloc] init];
                    qlview.chickStr = @"用户协议";
                    qlview.currentPreviewItemIndex = 0;
                    [self.navigationController pushViewController:qlview animated:YES];
                }else{//帮助和反馈
                    
                    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Storyboard" bundle:nil];
                    my_fankui *fankui = [storyboard instantiateViewControllerWithIdentifier:@"myFankui"];
                    [self.navigationController pushViewController:fankui animated:YES];
                }
                break;
            case 2://退出帐号
                {
                    [self btnClickOutLogin];
                }
            default:
                break;
        }
    }
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 53;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 20;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 1;
}

#pragma mark - 铃音开关
- (void)switckClick:(UISwitch *)sender{
    
    if (sender.isOn) {
        
        [USER_DEFAULTS setObject:@"YES" forKey:@"isOpenSound"];
        
    }else{
        
        [USER_DEFAULTS setObject:@"NO" forKey:@"isOpenSound"];
    }
}


#pragma mark - 注销
- (void)btnClickOutLogin
{
    //数据库删除
    NSArray *personArr = [Person MR_findAll];
    for (Person *tmp in personArr) {
        [tmp MR_deleteEntity];
    }
  [[NSManagedObjectContext MR_defaultContext] MR_saveToPersistentStoreAndWait];
    
//    [self resetDefaults];
    [USER_DEFAULTS setObject:@"1" forKey:@"token"];
    [USER_DEFAULTS removeObjectForKey:@"userid"];
    [USER_DEFAULTS removeObjectForKey:@"token"];
    [USER_DEFAULTS removeObjectForKey:@"industry"];
    [USER_DEFAULTS removeObjectForKey:@"imToken"];
    [USER_DEFAULTS removeObjectForKey:@"userName"];
    [USER_DEFAULTS removeObjectForKey:@"phone"];
    [USER_DEFAULTS removeObjectForKey:@"userid"];
    [USER_DEFAULTS removeObjectForKey:@"account"];
    [USER_DEFAULTS removeObjectForKey:@"usertype"];
    [USER_DEFAULTS removeObjectForKey:@"password"];
    [USER_DEFAULTS removeObjectForKey:@"address"];
    [USER_DEFAULTS removeObjectForKey:@"imToken"];
    [USER_DEFAULTS removeObjectForKey:@"headPhoto"];
    [USER_DEFAULTS removeObjectForKey:@"account"];
    [USER_DEFAULTS synchronize];
    
    [[RCIMClient sharedRCIMClient]
     logout];
    allConstMemberToken = nil;
    allConstMemberid = nil;
    
    self.tabBarController.selectedIndex = 0;

}
- (void)resetDefaults
{
    NSUserDefaults* defs = [NSUserDefaults standardUserDefaults];
    NSDictionary* dict = [defs dictionaryRepresentation];
    for(id key in dict) {
        [defs removeObjectForKey:key];
    }
    [defs synchronize];
}

#pragma mark - 解决表格线不到左边界
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
}


- (void)viewDidLayoutSubviews{
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [self.tableView setLayoutMargins:UIEdgeInsetsZero];
    }
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView setSeparatorInset:UIEdgeInsetsZero];
    }
}

@end
